package com.zzr.demo.redis.service.impl;

import com.zzr.demo.redis.controller.SocialRelationController;
import com.zzr.demo.redis.service.SocialRelationService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author: xiaomi
 * @date: 2021/10/8
 * @description:
 */
@Service
@Slf4j
public class SocialRelationServiceImpl implements SocialRelationService {

    /**
     * 正在关注
     */
    final String SOCIAL_ING_KEY = "social::following";
    /**
     * 关注自己
     */
    final String SOCIAL_ER_KEY = "social::follower";

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    /**
     * follow 关注 master
     *
     * @param follow
     * @param master
     */
    @Override
    public void follow(String follow, String master) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        //1.在 master 的 follower 集合增加 follow
        String k1 = SOCIAL_ER_KEY + "::" + master;
        Long add1 = opsForSet.add(k1, follow);
        log.info("add follower label:{}", add1);
        //2.在 follow 的 following 集合增加 master
        String k2 = SOCIAL_ING_KEY + "::" + follow;
        Long add2 = opsForSet.add(k2, master);
        log.info("add following label:{}", add2);
    }

    /**
     * 取消关注
     *
     * @param follow
     * @param master
     */
    @Override
    public void unfollow(String follow, String master) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        //1.在 master 的 follower 集合 移除 follow
        String k1 = SOCIAL_ER_KEY + "::" + master;
        Long add1 = opsForSet.remove(k1, follow);
        log.info("remove follower label:{}", add1);
        //2.在 follow 的 following 集合增加 master
        String k2 = SOCIAL_ING_KEY + "::" + follow;
        Long add2 = opsForSet.remove(k2, master);
        log.info("remove following label:{}", add2);
    }

    @Override
    public long getFollowingCount(String userId) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        String key = SOCIAL_ING_KEY + "::" + userId;
        Long size = opsForSet.size(key);
        log.info("{} following count:{}", userId, size);
        return size;
    }

    @Override
    public Set<String> getFollowings(String userId) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        String key = SOCIAL_ING_KEY + "::" + userId;
        Set<String> members = opsForSet.members(key);
        log.info("{} following members:{}", userId, members);
        return members;
    }

    @Override
    public Set<String> getCommonFollowings(String userId1, String userId2) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        String key1 = SOCIAL_ING_KEY + "::" + userId1;
        String key2 = SOCIAL_ING_KEY + "::" + userId2;
        Set<String> intersect = opsForSet.intersect(key1, key2);
        log.info("{}，{} 共同关注：{}", userId1, userId2, intersect);
        return intersect;
    }

    @Override
    public long getFollowerCount(String userId) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        String key = SOCIAL_ER_KEY + "::" + userId;
        Long size = opsForSet.size(userId);
        log.info("{} followers count:{}", userId, size);
        return size;
    }

    @Override
    public Set<String> getFollowers(String userId) {
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();
        String key = SOCIAL_ER_KEY + "::" + userId;
        Set<String> members = opsForSet.members(key);
        log.info("{} followers members:{}", userId, members);
        return members;
    }
}
